package com.example.demo.myrouteplan;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.demo.R;

import java.util.ArrayList;
import java.util.List;

public class ChooseActivity extends AppCompatActivity implements OnGetPoiSearchResultListener {

    private Button bt_dinzhi;
    private EditText et_days, et_start;
    private PoiSearch mPoiSearch = null;
    private String keyWordStr = null;

    private CheckBox bowuguan;
    private CheckBox guji;
    private CheckBox gongyuan;
    private CheckBox fengjingqu;
    private CheckBox youleyuan;
    private CheckBox qita;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose);
        et_days = findViewById(R.id.et_days);
        et_start = findViewById(R.id.et_start);
        bt_dinzhi = findViewById(R.id.bt_dinzhi);

        bowuguan = findViewById(R.id.bowuguan);
        guji = findViewById(R.id.guji);
        gongyuan = findViewById(R.id.gongyuan);
        fengjingqu = findViewById(R.id.fengjingqu);
        youleyuan = findViewById(R.id.youleyuan);
        qita = findViewById(R.id.qita);

        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(this);

        bt_dinzhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                keyWordStr = et_start.getText().toString();
                mPoiSearch.searchInCity((new PoiCitySearchOption())
                        .city("北京")
                        .keyword(keyWordStr)
                        .pageCapacity(20)
                        .pageNum(0) // 分页编号 仅能获取第mLoadIndex页的数据
                        .cityLimit(true)
                        .scope(1));//---------------------------------------------------------------------------
            }
        });
    }

    @Override
    public void onGetPoiResult(PoiResult poiResult) {
        Log.d("ChooseActivity", "获得poi结果 TotalPoiNum= " + poiResult.getTotalPoiNum() + " 总页数" + poiResult.getTotalPageNum());
        if (poiResult == null || poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
            Toast.makeText(ChooseActivity.this, "起点未找到结果", Toast.LENGTH_LONG).show();
            return;
        }

        List<PoiInfo> poiInfos = poiResult.getAllPoi();
        if (null == poiInfos) {
            return;
        } else {
            ArrayList<String> mytag = new ArrayList<String>();
            if (bowuguan.isChecked() ){
                mytag.add("博物馆");
            }
            if (guji.isChecked() ){
                mytag.add("古迹");
            }
            if (gongyuan.isChecked() ){
                mytag.add("公园");
            }
            if (fengjingqu.isChecked() ){
                mytag.add("风景区");
            }
            if (youleyuan.isChecked() ){
                mytag.add("游乐园");
            }
            if (qita.isChecked() ){
                mytag.add("其他");
            }
            double lat=poiInfos.get(0).location.latitude;
            double lng=poiInfos.get(0).location.longitude;
            String name=poiInfos.get(0).getName();

            Toast.makeText(this, poiInfos.get(0).getName(), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(ChooseActivity.this, PlanShowActivity.class);
            intent.putExtra("days",Integer.valueOf(et_days.getText().toString()));
            intent.putExtra("lat", lat);
            intent.putExtra("lng", lng);
            intent.putExtra("name",name);

            intent.putStringArrayListExtra("mytag", mytag);

            startActivity(intent);
        }

    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

    }

    @Override
    public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {

    }

    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

    }
}